package com.sanshow.charging;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;

import com.liulishuo.filedownloader.FileDownloader;
import com.liulishuo.filedownloader.connection.FileDownloadUrlConnection;
import com.sanshow.charging.db.DaoSession;
import com.sanshow.charging.db.DbController;
import com.sanshow.charging.util.CrashHandler;
import com.sanshow.charging.util.CrashHandler_Ma;

import java.util.LinkedList;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import me.f1reking.serialportlib.SerialPortHelper;

public class MyApplication extends Application {

    private static Context context;
    private static DaoSession daoSession;
    private static SerialPortHelper mSerialPortHelper;
    private static BlockingQueue<String> mBackOpenDoorTaskQueue = new LinkedBlockingQueue<>();

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        CrashHandler_Ma.getInstance().init(this);

        FileDownloader.setupOnApplicationOnCreate(this)
                .connectionCreator(new FileDownloadUrlConnection
                        .Creator(new FileDownloadUrlConnection.Configuration()
                        .connectTimeout(15_000) // set connection timeout.
                        .readTimeout(15_000) // set read timeout.
                ))
                .commit();

        initDb();
    }

    /**
     * 初始化数据库创建
     */
    private void initDb() {
        daoSession = DbController.getInstance(context).getDaoSession();
    }

    /**
     * 获得数据库对象
     * @return
     */
    public static DaoSession getDaoInstant() {
        return daoSession;
    }


    public static Context getContext() {
        return context;
    }

    public SerialPortHelper setSerialPortHelper(SerialPortHelper serialPortHelper){
        return mSerialPortHelper = serialPortHelper;
    }

    public static SerialPortHelper getSerialPortHelper(){
        return mSerialPortHelper;
    }

    public static BlockingQueue<String> getBackOpenDoorTaskQueue(){
        return mBackOpenDoorTaskQueue;
    }

    public static void setBackOpenDoorTaskQueue(String itemCode){
        mBackOpenDoorTaskQueue.add(itemCode);
    }

}
